import { deleteUseLessObjectKey, filterObj } from 'billd-utils';
import { Op, literal } from 'sequelize';

import { IGiftRecord, IList } from '@/interface';
import giftModel from '@/model/giftRecord.model';
import { handleGroupPaging, handlePaging } from '@/utils';

class GiftRecordService {
  /** 礼物记录是否存在 */
  async isExist(ids: number[]) {
    const res = await giftModel.count({
      where: {
        id: {
          [Op.in]: ids,
        },
      },
    });
    return res === ids.length;
  }

  /** 获取礼物记录列表 */
  async getList({
    id,
    is_recv,
    goods_id,
    goods_nums,
    order_id,
    live_room_id,
    send_user_id,
    recv_user_id,
    status,
    orderBy,
    orderName,
    nowPage,
    pageSize,
    keyWord,
    rangTimeType,
    rangTimeStart,
    rangTimeEnd,
  }: IList<IGiftRecord>) {
    let offset;
    let limit;
    if (nowPage && pageSize) {
      offset = (+nowPage - 1) * +pageSize;
      limit = +pageSize;
    }
    const allWhere: any = deleteUseLessObjectKey({
      id,
      is_recv,
      goods_id,
      goods_nums,
      order_id,
      live_room_id,
      send_user_id,
      recv_user_id,
      status,
    });
    if (keyWord) {
      const keyWordWhere = [
        {
          goods_snapshot: {
            [Op.like]: `%${keyWord}%`,
          },
        },
        {
          remark: {
            [Op.like]: `%${keyWord}%`,
          },
        },
      ];
      allWhere[Op.or] = keyWordWhere;
    }
    if (rangTimeType && rangTimeStart && rangTimeEnd) {
      allWhere[rangTimeType] = {
        [Op.gt]: new Date(+rangTimeStart),
        [Op.lt]: new Date(+rangTimeEnd),
      };
    }
    const orderRes: any[] = [];
    if (orderName && orderBy) {
      orderRes.push([orderName, orderBy]);
    }
    const result = await giftModel.findAndCountAll({
      order: [...orderRes],
      limit,
      offset,
      where: {
        ...allWhere,
      },
    });
    return handlePaging(result, nowPage, pageSize);
  }

  /** 获取礼物记录列表 */
  async getGiftGroupList({
    id,
    is_recv,
    goods_id,
    goods_nums,
    order_id,
    live_room_id,
    send_user_id,
    recv_user_id,
    status,
    orderBy,
    orderName,
    nowPage,
    pageSize,
    keyWord,
    rangTimeType,
    rangTimeStart,
    rangTimeEnd,
  }: IList<IGiftRecord>) {
    let offset;
    let limit;
    if (nowPage && pageSize) {
      offset = (+nowPage - 1) * +pageSize;
      limit = +pageSize;
    }
    const allWhere: any = deleteUseLessObjectKey({
      id,
      is_recv,
      goods_id,
      goods_nums,
      order_id,
      live_room_id,
      send_user_id,
      recv_user_id,
      status,
    });
    if (keyWord) {
      const keyWordWhere = [
        {
          goods_snapshot: {
            [Op.like]: `%${keyWord}%`,
          },
        },
        {
          remark: {
            [Op.like]: `%${keyWord}%`,
          },
        },
      ];
      allWhere[Op.or] = keyWordWhere;
    }
    if (rangTimeType && rangTimeStart && rangTimeEnd) {
      allWhere[rangTimeType] = {
        [Op.gt]: new Date(+rangTimeStart),
        [Op.lt]: new Date(+rangTimeEnd),
      };
    }
    const orderRes: any[] = [];
    if (orderName && orderBy) {
      orderRes.push([orderName, orderBy]);
    }
    console.log(allWhere);
    const result = await giftModel.findAndCountAll({
      attributes: [
        'live_room_id',
        [literal(`count(goods_id)`), 'nums'],
        [literal(`GROUP_CONCAT(DISTINCT goods_id)`), 'goods_id'],
        // [
        //   literal(
        //     `${
        //       '(SELECT goods_snapshot FROM gift_record WHERE goods_id IN ' +
        //       '(SELECT goods_id FROM gift_record WHERE live_room_id = '
        //     }${giftModel.escape(
        //       allWhere.live_room_id
        //     )} GROUP BY goods_id LIMIT 1)`
        //   ),
        //   'goods_snapshot',
        // ],
      ],
      order: [...orderRes],
      limit,
      offset,
      group: ['goods_id'],
      where: {
        ...allWhere,
      },
      raw: true,
    });
    return handleGroupPaging(result, nowPage, pageSize);
  }

  /** 查找礼物记录 */
  async find(id: number) {
    const result = await giftModel.findOne({ where: { id } });
    return result;
  }

  /** 修改礼物记录 */
  async update(data: IGiftRecord) {
    const { id } = data;
    const data2 = filterObj(data, ['id']);
    const result = await giftModel.update(data2, { where: { id } });
    return result;
  }

  /** 创建礼物记录 */
  async create(data: IGiftRecord) {
    const result = await giftModel.create(data);
    return result;
  }

  /** 删除礼物记录 */
  async delete(id: number) {
    const result = await giftModel.destroy({
      where: { id },
      individualHooks: true,
    });
    return result;
  }
}

export default new GiftRecordService();
